package com.yk.i_wms.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yk.i_wms.common.model.PageResult;
import com.yk.i_wms.common.model.Result;
import com.yk.i_wms.dto.SmsReturnDocDTO;
import com.yk.i_wms.model.SmsReturnDoc;
import com.yk.i_wms.model.SmsReturnDocDetail;
import com.yk.i_wms.model.SmsReturnDocRecord;
import com.yk.i_wms.service.ISmsReturnDocDetailService;
import com.yk.i_wms.service.ISmsReturnDocRecordService;
import com.yk.i_wms.service.ISmsReturnDocService;
import com.yk.i_wms.util.EasyPoiExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 退料入库单
 *
 * @author hbq
 * @date 2021-04-14 13:37:08
 */
@Slf4j
@CrossOrigin
@RestController
@Api(tags = "退料入库单")
@RequestMapping("sms")
public class SmsReturnDocController {
    @Autowired
    private ISmsReturnDocService smsReturnDocService;
    @Autowired
    private ISmsReturnDocDetailService smsReturnDocDetailService;
    @Autowired
    private ISmsReturnDocRecordService smsReturnDocRecordService;
    /**
     * 列表
     */
    @ApiOperation(value = "查询列表")
    @PostMapping("/smsReturnDoc/list")
    public Result<PageResult> list(@RequestBody Map<String, Object> params) {
        Page<Map> list= smsReturnDocService.findList(params);
        return Result.succeed(PageResult.restPage(list),"查询成功");
    }

    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @PostMapping("/smsReturnDoc/sel/{id}")
    public Result findUserById(@PathVariable Long id) {
        SmsReturnDoc model = smsReturnDocService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新(包含详情)
     */
    @ApiOperation(value = "新增or更新(包含详情)")
    @PostMapping("/smsReturnDoc/saveAll")
    public Result saveAll(@RequestBody SmsReturnDocDTO smsReturnDocDTO) {
        return smsReturnDocService.saveAll(smsReturnDocDTO);
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "新增or更新")
    @PostMapping("/smsReturnDoc/save")
    public Result save(@RequestBody SmsReturnDoc smsReturnDoc) {
        smsReturnDocService.saveOrUpdate(smsReturnDoc);
        return Result.succeed("保存成功");
    }

    /**
     * 批量新增or更新
     */
    @ApiOperation(value = "批量新增or更新")
    @PostMapping("/smsReturnDoc/saveBatch")
    public Result saveBatch(@RequestBody Map<String,List<SmsReturnDoc>> map) {
        List<SmsReturnDoc> models = map.get("models");
        smsReturnDocService.saveOrUpdateBatch(models);
        return Result.succeed("保存成功");
    }

    /**
     * 根据单号查详情
     */
    @ApiOperation(value = "根据单号查详情")
    @PostMapping("/smsReturnDoc/selAllByDocNo")
    public Result selAllByDocNo(@RequestBody Map<String, Object> params) {
        String docNo = MapUtil.getStr(params, "docNo");
        SmsReturnDoc returnDoc = smsReturnDocService.getOne(new LambdaQueryWrapper<SmsReturnDoc>()
                .eq(SmsReturnDoc::getDocNo, docNo));
        Map<String, Object> resultMap = BeanUtil.beanToMap(returnDoc);
        List<SmsReturnDocDetail> returnDocDetails = smsReturnDocDetailService.list(new LambdaQueryWrapper<SmsReturnDocDetail>().eq(SmsReturnDocDetail::getDocNo, docNo));
        resultMap.put("returnDocDetails",returnDocDetails);
        return Result.succeed(resultMap, "查询成功");
    }

    /**
     * 根据详情id查sn记录
     */
    @ApiOperation(value = "根据详情id查sn记录")
    @PostMapping("/smsReturnDoc/selRecordByDdId")
    public Result selRecordByDdid(@RequestBody Map<String, Object> params) {
        String ddId = MapUtil.getStr(params, "ddId");
        List<SmsReturnDocRecord> returnDocRecords = smsReturnDocRecordService.list(new LambdaQueryWrapper<SmsReturnDocRecord>()
                .eq(SmsReturnDocRecord::getDdId, ddId));
        return Result.succeed(returnDocRecords, "查询成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "批量删除")
    @PostMapping("/smsReturnDoc/del")
    public Result delete(@RequestBody Map<String,List<Long>> map) {
        return smsReturnDocService.delete(map);
    }

    /**
     * 关结
     */
    @ApiOperation(value = "关结")
    @PostMapping("/smsReturnDoc/close")
    public Result close(@RequestBody SmsReturnDoc smsReturnDoc) {
        return smsReturnDocService.close(smsReturnDoc);
    }

    /**
     * 根据sn查sn的记录
     */
    @ApiOperation(value = "根据sn查sn的记录")
    @PostMapping("/smsReturnDoc/selRecordBySn")
    public Result selRecordBySn(@RequestBody Map<String, Object> params) {
        return smsReturnDocService.selRecordBySn(params);
    }

    /**
     * 修改库存数据
     */
    @ApiOperation(value = "修改库存数据")
    @PostMapping("/smsReturnDoc/updateStock")
    public Result updateStock(@RequestBody Map<String, Object> params) {
        return smsReturnDocService.updateStock(params);
    }


    /**
     * 导入
     */
    @ApiOperation(value = "导入")
    @PostMapping("/smsReturnDoc/leadIn")
    public  Result leadIn(MultipartFile excel) throws Exception {
        int rowNum = 0;
        if (!excel.isEmpty()) {
            List<SmsReturnDoc> list = EasyPoiExcelUtil.importExcel(excel, 1, 1, SmsReturnDoc.class);
            rowNum = list.size();
            if (rowNum > 0) {
                list.forEach(u -> {
                        smsReturnDocService.save(u);
                });
                return Result.succeed("成功导入信息"+rowNum+"行数据");
            }
        }
        return Result.failed("导入失败");
    }
    
    /**
     * 导出（传入ids数组，选择指定id）
     */
    @ApiOperation(value = "导出（传入ids数组，选择指定id）")
    @PostMapping("/smsReturnDoc/leadOut")
    public void leadOut(@RequestBody Map<String,List<Long>> map,HttpServletResponse response) throws IOException {
        List<Long> ids = map.get("ids");
        List<SmsReturnDoc> smsReturnDocList = ids==null||ids.isEmpty()? smsReturnDocService.list():(List)smsReturnDocService.listByIds(ids);
        //导出操作
        EasyPoiExcelUtil.exportExcel(smsReturnDocList, "退料入库单导出", "退料入库单导出", SmsReturnDoc.class, "smsReturnDoc.xls", response);
    }
}
